package com.clayecloud.common.admin.services.Impl;

import com.clayecloud.common.admin.services.HouseService;
import com.clayecloud.common.entity.House;
import com.clayecloud.common.mappers.HouseMapper;
import com.clayecloud.common.mappers.HouseUserMapper;
import com.clayecloud.common.utils.MessageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author  ClayeLink
 * @date  2021/12/31 14:51
 */
@Service("AdminHouseServiceImpl")
public class HouseServiceImpl implements HouseService {

    @Resource
    private HouseMapper houseMapper;

    @Resource
    private HouseUserMapper houseUserMapper;

    @Override
    public MessageUtil deleteByPrimaryKey(Integer houseId) {
        return houseMapper.deleteByPrimaryKey(houseId) > 0 && houseUserMapper.deleteByPrimaryKey(houseId) > 0 ? MessageUtil.success("房屋删除成功") : MessageUtil.fail("删除失败，请联系管理员");
    }

    @Override
    public int insert(House record) {
        return houseMapper.insert(record);
    }

    @Override
    public int insertSelective(House record) {
        return houseMapper.insertSelective(record);
    }

    @Override
    public House selectByPrimaryKey(Integer houseId) {
        return houseMapper.selectByPrimaryKey(houseId);
    }

    @Override
    public MessageUtil updateByPrimaryKeySelective(House record) {
        return houseMapper.updateByPrimaryKeySelective(record) > 0 ? MessageUtil.success("修改成功") : MessageUtil.fail("修改失败，请联系管理员");
    }

    @Override
    public int updateByPrimaryKey(House record) {
        return houseMapper.updateByPrimaryKey(record);
    }

    @Override
    public MessageUtil<List<House>> selectAll(Integer size, Integer page) {
        PageHelper.startPage(page,size);
        List<House> houses = houseMapper.selectAll();
        PageInfo pageInfo = new PageInfo(houses);
        Map result = new HashMap();
        result.put("list",houses);
        result.put("total",pageInfo.getTotal());
        return MessageUtil.success("查询成功").add(result);
    }

}
